package com.devMode.DecoratorMode;

/**
 * 装饰器模式
 * 装饰者和具体被装饰的组件都继承于同一组件
 */

/**
 * 设计不同类型的饮料，饮料可以添加配料，
 * 比如可以添加牛奶，并且每添加一次价格就会上涨一次，要求计算一种饮料的价格
 */
public class DecoratorMode {
    public static void main(String[] args) {
        Beverage beverage = new HouseBlend();
        beverage = new Mocha(beverage);
        beverage = new Milk(beverage);
        System.out.println(beverage.cost());
    }
}
interface Beverage{
    double cost();
}

class DarkRoast implements Beverage{

    @Override
    public double cost() {
        return 1;
    }
}

class HouseBlend implements Beverage{

    @Override
    public double cost() {
        return 2;
    }
}
abstract class CondimentDecorator implements Beverage{
    protected Beverage beverage;
}

class Milk extends CondimentDecorator{

    public Milk(Beverage beverage){
        this.beverage = beverage;
    }

    @Override
    public double cost() {
        return 1 + beverage.cost();
    }
}

class Mocha extends CondimentDecorator{

    public Mocha(Beverage beverage){
        this.beverage = beverage;
    }

    @Override
    public double cost() {
        return 1 + beverage.cost();
    }
}

